//
//  SNGiftAndPromotionView.m
//  app
//
//  Created by 王少宁 on 15/6/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNGiftAndPromotionView.h"

@interface SNGiftAndPromotionView ()
@property (nonatomic, retain) UIImageView * rightImgView;
@property (nonatomic, retain) UIButton * mainButton;


//单礼物
@property (nonatomic, strong) UILabel *giftLabel;
@property (nonatomic, strong) SNGiftModel *giftModel;
@end

@implementation SNGiftAndPromotionView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectOf([self.ufo fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH oh:kGiftViewHeight]);

        [self cretateRightImgView];
        
        self.giftLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP], [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin fh:UB_HEIGHT])];
        self.giftLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
        self.giftLabel.font = JKAppGlobleFontMinTagText;
        [self addSubview:self.giftLabel];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonClick)];
        self.giftLabel.userInteractionEnabled = YES;
        [self.giftLabel addGestureRecognizer:tapGesture];
    }
    
    return self;
}

//仅仅显示赠送的礼物
- (void)showViewWithOriginY:(CGFloat)y giftModel:(SNGiftModel *)giftModel{
    self.frame = CGRectMake(0, y, SCREEN_WIDTH, kGiftViewHeight);
    self.giftModel = giftModel;
    
    self.giftLabel.text = [NSString stringWithFormat:@"【赠品】 %@", giftModel.title];;
}

/********************************即显示赠品又显示促销信息******************************/

- (instancetype)initWithOriginY:(CGFloat)y giftModel:(SNGiftModel *)giftModel promotionModel:(SNPromotionModel *)promotionModel
{
    if ([super init]) {
        if (giftModel.gift && promotionModel.promotion) {//有赠品和促销活动
            self.frame = CGRectMake(0, y, SCREEN_WIDTH, kGiftViewHeight*2);
            [self cretateMainButton];
            [self cretateRightImgView];
            [self createViewWithGiftModel:giftModel promotionModel:promotionModel];
            return self;
        } else if (giftModel.gift || promotionModel.promotion) {//有赠品或促销活动其中的一个
            self.frame = CGRectMake(0, y, SCREEN_WIDTH, kGiftViewHeight*2);
            [self cretateMainButton];
            [self cretateRightImgView];
            [self createViewWithGiftModel:giftModel promotionModel:promotionModel];
            return self;
        } else {////赠品促销活动都没有
            return nil;
        }
    } else {
        return nil;
    }
}

- (void)showViewWithOriginY:(CGFloat)y giftModel:(SNGiftModel *)giftModel promotionModelArray:(NSArray *)promotionModelArray{
    
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    NSInteger col = 0;
    if (giftModel || promotionModelArray.count) {
        if (giftModel) {
            col = 1;
        }
        if (promotionModelArray.count) {
            col += 1;
        }
        self.frame = CGRectMake(0, y, SCREEN_WIDTH, kGiftViewHeight*(col));
        
        [self cretateRightImgView];
        [self createViewWithGiftModel:giftModel promotionModelArray:promotionModelArray];
        
        [self cretateMainButton];
        
        self.hidden = NO;
    }else{
        self.frame = CGRectMake(0, y, SCREEN_WIDTH, 0);
        self.hidden = YES;
    }
}


- (void)cretateMainButton
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = self.bounds;
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

- (void)buttonClick
{
    if(self.targetCallerShowGiftList){
        [self.targetCallerShowGiftList call];
    }
}

- (void)cretateRightImgView
{
    _rightImgView = CREATE_WITH_FRAME_OF(UIImageView, CGPointMake(SCREEN_WIDTH - 25, self.ubo.height*0.5-10),
                                         [self.ubo ow:10 oh:20]);
    _rightImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mall_right_s_arrow_oringe@2x" ofType:@"png"]];
    _rightImgView.contentMode = UIViewContentModeRight;
    [self addSubview:_rightImgView];
}

- (void)createViewWithGiftModel:(SNGiftModel *)giftModel promotionModelArray:(NSArray *)promotionModelArray
{
    if (giftModel.gift && !promotionModelArray.count) {//只有赠品
        UILabel * giftLabel = [self createLabelWithFrame:CGRectOf([self.ubo ux:kSizeMallViewMargin/2 fy:0],
                                                                  [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:kGiftViewHeight])];
        giftLabel.text = [NSString stringWithFormat:@"【赠品】 %@",giftModel.title];
   
    } else if (!giftModel.gift && promotionModelArray.count) {//只有促销活动
        NSInteger col = 0;
        for (SNPromotionModel *model in promotionModelArray) {
            UILabel * promotionLabel = [self createLabelWithFrame:CGRectOf([self.ubo ux:kSizeMallViewMargin/2 oy:col*kGiftViewHeight],
                                                                           [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:kGiftViewHeight])];
            promotionLabel.text = [NSString stringWithFormat:@"【%@】 %@",model.name,model.title];
            col ++;
        }
        
    } else if (giftModel.gift && promotionModelArray.count) {//既有赠品又有促销活动
        UILabel * giftLabel = [self createLabelWithFrame:CGRectOf([self.ubo ux:kSizeMallViewMargin/2 fy:0],
                                                                  [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:kGiftViewHeight])];
        giftLabel.text = [NSString stringWithFormat:@"【赠品】 %@",giftModel.title];
        
        NSInteger col = 1;
        for (SNPromotionModel *model in promotionModelArray) {
            UILabel * promotionLabel = [self createLabelWithFrame:CGRectOf([self.ubo ux:kSizeMallViewMargin/2 oy:col*kGiftViewHeight],
                                                                           [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:kGiftViewHeight])];
            promotionLabel.text = [NSString stringWithFormat:@"【%@】 %@",model.name,model.title];
            break;
        }
    }
    
    self.rightImgView.frame = CGRectOf([self.ubo fx:UB_RIGHT ux:-kSizeMallViewMargin ox:-10 fy:UB_CENTER oy:-10], [self.ubo ow:10 oh:20]);
}

- (void)createViewWithGiftModel:(SNGiftModel *)giftModel promotionModel:(SNPromotionModel *)promotionModel
{
    if (giftModel.gift && !promotionModel.promotion) {//只有赠品
        UILabel * giftLabel = [self createLabelWithFrame:CGRectOf([self.ubo fx:0 fy:0],
                                                                  [self.ubo ow:SCREEN_WIDTH-30 fh:1.0])];
        giftLabel.text = [NSString stringWithFormat:@"【赠品】 %@",giftModel.title];
    } else if (!giftModel.gift && promotionModel.promotion) {//只有促销活动
        UILabel * promotionLabel = [self createLabelWithFrame:CGRectOf([self.ubo fx:0 fy:0],
                                                                       [self.ubo ow:SCREEN_WIDTH-30 fh:1.0])];
        promotionLabel.text = [NSString stringWithFormat:@"【%@】 %@",promotionModel.name,promotionModel.title];
    } else if (giftModel.gift && promotionModel.promotion) {//既有赠品又有促销活动
        UILabel * giftLabel = [self createLabelWithFrame:CGRectOf([self.ubo fx:0 fy:0],
                                                   [self.ubo ow:SCREEN_WIDTH-30 oh:kGiftViewHeight])];
        giftLabel.text = [NSString stringWithFormat:@"【赠品】 %@",giftModel.title];
        
        UILabel * promotionLabel = [self createLabelWithFrame:CGRectOf([giftLabel.ubo fx:0 fy:1.0],
                                                        [giftLabel.ubo fw:1.0 fh:1.0])];
        promotionLabel.text = [NSString stringWithFormat:@"【%@】 %@",promotionModel.name,promotionModel.title];
    }
}

- (UILabel *)createLabelWithFrame:(CGRect)frame
{
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    label.font = JKAppGlobleFontMinTagText;
    [self addSubview:label];
    return label;
}

@end
